const arr = ['CNY : 1314', 'CNY : 520.1314', 'TUI : 530'];
const arr2 = ['CNY : 1314', 'USD : 520.1314', 'TUI : 520']
function someSum (arr) {
  let map = {};
  arr.forEach(e => {
    let temp = e.split(":");
    if (temp.length === 2) {
      const key = temp[0].trim();
      const num = Number(temp[1].trim());
      if (map[key]) {
        // 属性存在
        map[key] = map[key] + num;
      } else {
        // 属性不存在
        map[key] = num;
      }
    }
  });
  return map;
}

// console.log(someSum(arr),someSum(arr2));



const arr3 = [{ id: 1, name: 520 }, { id: 2, name: 1314 }, { id: 1, name: 1314520 }];

function someSum2 (arr) {
  let map = {};
  arr.forEach(e => {
    const { id, name } = e;
    if (id) {
      if (map[id]) {
        // 属性存在
        map[id] = map[id] + name;
      } else {
        // 属性不存在
        map[id] = name;
      }
    }
  });
  return toArray(map, 'id', 'name');
}

function toArray (map, key1, key2) {
  // let arr
  // for (const [key, value] of Object.entries(map)) {
  //   console.log(`${key}: ${value}`);
  // }
  return Object.entries(map).map(([key, value]) => ({ [key1]: Number(key), [key2]: value }))
}
console.log(someSum2(arr3));
